package com.example.fireassistant_server.controller;

import com.example.fireassistant_server.dto.Essential.EssentialCreateRequest;
import com.example.fireassistant_server.dto.Essential.EssentialSearchFilter;
import com.example.fireassistant_server.dto.Essential.EssentialUpdateRequest;
import com.example.fireassistant_server.mapper.EssentialMapper;
import com.example.fireassistant_server.service.EssentialService;
import com.example.fireassistant_server.vo.EssentialVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/essential")
public class EssentialController {

    EssentialMapper essentialMapper;

    EssentialService essentialService;

    @RequestMapping(method = RequestMethod.POST)
    EssentialVo create(@Validated @RequestBody EssentialCreateRequest essentialCreateRequest) {
        return essentialMapper.toVo(essentialService.create(essentialCreateRequest));
    }

    @PutMapping("/{id}")
    EssentialVo update(@PathVariable String id,
                  @Validated @RequestBody EssentialUpdateRequest essentialUpdateRequest) {
        return essentialMapper.toVo(essentialService.update(id, essentialUpdateRequest));
    }

    @GetMapping("/findByNameLike/{keyword}")
    @ApiOperation("内容检索")
    List<EssentialVo> findByKeywordLike(@PathVariable String keyword) {
        return essentialService.findByKeywordLike("%"+keyword+"%").stream().map(essentialMapper::toVo).collect(Collectors.toList());
    }


    @PostMapping("/search")
    @ApiOperation("用户检索")
    Page<EssentialVo> searchAll(@Validated @RequestBody EssentialSearchFilter essentialSearchFilter) {
        return essentialService.searchAll(essentialSearchFilter).map(essentialMapper::toVo);
    }

    @GetMapping("/{id}")
    EssentialVo getById(@PathVariable String id) {
        return essentialMapper.toVo(essentialService.getById(id));
    }



    @DeleteMapping("/{id}")
    void deleteById(@PathVariable String id) {
        essentialService.deleteById(id);
    }


    @Autowired
    public void setEssentialMapper(EssentialMapper essentialMapper) {
        this.essentialMapper = essentialMapper;
    }

    @Autowired
    public void setEssentialService(EssentialService essentialService) {
        this.essentialService = essentialService;
    }
}
